package compiler;

import java.io.*;
import java.util.Arrays;

/**
 * @author tjc
 * @date 2020-3-9
 */
public class Compiler {
    private static char[] src;
    Lexer lexer;

    public static void main(String[] args) {
        String path = "demo/demo2.txt";
        String path2 = "E:\\IntelliJ IDEA 2019.2.3\\workspace\\compiler\\src\\demo\\demo2.txt";
        Compiler compiler = new Compiler();
        compiler.compile(path2);

    }



    public void compile(String file){
        try {
            src = loadSrc(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Parser parser = new Parser(lexer);
        parser.lexer = new Lexer(src);
        //tokenList.stream().forEach(System.out::println);
        parser.parse();
    }


    //相对路径加载源文件
    public char[] loadSrc(String file) throws IOException {
        //TODO src容量是固定的，代码文件大一点就不行了
        String srcPath = Lexer.class.getClassLoader().getResource(file).getFile();
        char[] src = new char[2048];
        int byteCount = new BufferedReader(new InputStreamReader(new FileInputStream(new File(srcPath)))).read(src, 0, src.length - 1);
        char[]src1 = Arrays.copyOf(src, byteCount);
        return src1;
    }

    //绝对路径加载源文件
    public char[] loadSrc2(String file) throws IOException {
        char[] src = new char[2048];
        int byteCount = new BufferedReader(new InputStreamReader(new FileInputStream(new File(file)))).read(src, 0, src.length - 1);
        char[] src1 = Arrays.copyOf(src, byteCount);
        return src1;
    }
}
